import request from "../utils/request";

/**
 * 获取首页轮播图
 */
export function findBanner() {
  return request({
    url: "/index/findBanner",
  });
}

/**
 * 获取一级分类
 */
export function findCategory1() {
  return request({
    url: "/index/findCategory1",
  });
}

//猜你喜欢
export function findListGoods() {
  return request({
    url: "/index/findListGoods",
  });
}

//人气推荐
export function findRecommendGoods() {
  return request({
    url: "/index/findRecommendGoods",
  });
}

// 获取商品列表分页数据
// params = {
//   limit,
//   page,
//   sort,
//   category1Id,
//   category2Id,
//   keyword,
// };
export function findGoodsListByPage(params) {
  //通过Object.assign方法来合并对象,后面的对象覆盖前面的
  const data = Object.assign(
    {
      limit: 10,
      page: 1,
      sort: "",
      category1Id: "",
      category2Id: "",
      keyword: "",
    },
    params
  );
  let query = {};
  // 把 data 中除了 limit\page 以外的值不为空的数据 赋值到 query 对象中
  for (let key in data) {
    if (key != "limit" && (key != "page") & (data[key] != "")) {
      query[key] = data[key];
    }
  }
  return request({
    url: `/goods/list/${data.page}/${data.limit}`,
    data: query,
  });
}

//获取分类层级数据
export function findCategoryTree() {
  return request({
    url: "/index/findCategoryTree",
  });
}

//获取指定id的商品详情
export function getGoodsDetail(id) {
  return request({
    url: `/goods/${id}`,
  });
}

/**
 * 添加购物车商品
 */
export function addToCart(goodsId, count, blessing = "") {
  return request({
    url: `/cart/addToCart/${goodsId}/${count}`,
    data: {
      blessing,
    },
  });
}

/**
 * 微信登录
 */
export function wxLogin(code) {
  return request({
    url: `/weixin/wxLogin/${code}`,
  });
}
